//
//  RequsetManager.m
//  Leisure
//
//  Created by lanou on 16/6/28.
//  Copyright © 2016年 张凡. All rights reserved.
//

#import "RequsetManager.h"

@implementation RequsetManager

//请求数据,根据类型会自动识别
+ (void)fetchDataWithUrl:(NSString *)url parametr:(NSDictionary *)parametr mehtod:(NSString *)method complete:(ReauqestComplete)complete updateUI:(UpdateUI)update{
    
    //创建请求对象,并设置请求对象的属性
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //创建子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        if ([method isEqualToString:@"GET"]) {
            
            [manager GET:url parameters:parametr progress:^(NSProgress * _Nonnull downloadProgress) {
                
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                //把data传出去
                complete((NSData *)responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    //刷新UI
                    update();
                });
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                
            }];
        }else{
            
            [manager POST:url parameters:parametr progress:^(NSProgress * _Nonnull uploadProgress) {
                
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                //把data传出去
                complete((NSData *)responseObject);
                //回到主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    //刷新UI
                    update();
                });
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                
            }];
        }
       
    });
    
    
}


@end
